import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import java.io.InputStream

object ImageDownloader {

    private val client = OkHttpClient()

    fun downloadBitmapFromUrl(url: String): Bitmap? {
        try {
            // 创建一个请求
            val request = Request.Builder().url(url).build()

            // 执行请求
            val response = client.newCall(request).execute()

            // 检查响应是否成功
            if (response.isSuccessful && response.body != null) {
                // 获取响应体的输入流
                val inputStream: InputStream = response.body!!.byteStream()

                // 将输入流转换为Bitmap
                return BitmapFactory.decodeStream(inputStream)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }
}